package com.xhs.main.service.yfb.impl;

import com.xhs.main.dao.yfb.YfbUserAuthorityMapper;
import com.xhs.main.dao.yfb.YfbUserMapper;
import com.xhs.main.entity.yfb.YfbUser;
import com.xhs.main.entity.yfb.YfbUserAuthority;
import com.xhs.main.entity.yfb.YfbUserAuthorityExample;
import com.xhs.main.entity.yfb.YfbUserExample;
import com.xhs.main.service.yfb.YfbUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashSet;
import java.util.List;
import java.util.Set;

/**
 * @create 2020-10-24 19:04
 */
@Service
public class YfbUserServiceImpl implements YfbUserService {
    @Autowired
    YfbUserMapper yfbUserMapper;
    @Autowired
    YfbUserAuthorityMapper yfbUserAuthorityMapper;


    //根据用户名查用户（用户名是不重复的）
    @Override
    public YfbUser queryUserByName(String username) {
        YfbUserExample example = new YfbUserExample();
        YfbUserExample.Criteria criteria = example.createCriteria();
        criteria.andYfbUserUsernameEqualTo(username);
        List<YfbUser> yfbUsers = yfbUserMapper.selectByExample(example);
        if(yfbUsers.size() == 0) {
            return null;
        } else {
            return yfbUsers.get(0);
        }
    }

    //查用户的权限
    @Override
    public Set<String> getPermissions(String username) {
        YfbUserAuthorityExample example = new YfbUserAuthorityExample();
        YfbUserAuthorityExample.Criteria criteria = example.createCriteria();
        criteria.andYfbURUsernumberEqualTo(username);
        List<YfbUserAuthority> yfbUserAuthorities = yfbUserAuthorityMapper.selectByExample(example);
        Set<String> permissions = new HashSet<>();
        for (int i = 0; i < yfbUserAuthorities.size(); i++) {
            permissions.add(yfbUserAuthorities.get(i).getYfbURRolenumber());
        }
        return permissions;
    }


}
